package com.dems.core.log;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.Map;

public class TraceIdContext {

    /**
     * TRACE_ID_PREFIX: X-
     */
    public static final String TRACE_ID_PREFIX = "X-";

    /**
     * requestId
     */
    public static final String TRACE_ID_KEY = "requestId";

    /**
     * 阿里鹰眼Id
     */
    public static final String EAGLE_ID_KEY = "EagleId";

    public static void setTraceId(String traceId) {
        if (StringUtils.isNotEmpty(traceId)) {
            MDC.put(TRACE_ID_KEY, traceId);
        }
    }

    public static String getTraceId() {
        String traceId = MDC.get(TRACE_ID_KEY);
        return StringUtils.isBlank(traceId) ? TraceIdUtils.getTraceId() : traceId;
    }

    public static void removeTraceId() {
        MDC.remove(TRACE_ID_KEY);
    }

    public static void clearTraceId() {
        MDC.clear();
    }

    public static Map<String, String> getContextMap() {
        return MDC.getCopyOfContextMap();
    }

}